/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "OutputParser.h"

#include <QtCore/QIODevice>
#include <QtCore/QCoreApplication>

using namespace Bio;

const qint64	c_bufferSize = 32768; // 32 Kbytes

/**
**/
OutputParser::OutputParser( QIODevice &output ) : m_output( output )
{
}

/**
**/
OutputParser::~OutputParser()
{
}

/**
**/
bool OutputParser::parse( QIODevice &dev )
{
	bool	closeDevice = false;

	if( !m_output.isOpen() ){
		if( !m_output.open( QIODevice::WriteOnly ) )
			return false;

		closeDevice = true;
	}

	char	*buffer = new char [c_bufferSize];

	while( !dev.atEnd() ){
		qint64	bytesRead = dev.read( buffer, c_bufferSize );

		m_output.write( buffer, bytesRead );
	}

	delete buffer;
	
	if( closeDevice )
		m_output.close();

	return true;
}
